import React, { Component, Fragment } from "react";

import News from "./News";
import Phone from "./Phone";

import axios from "axios";

import { store } from "../redux/store";
import * as action from "../redux/action";
export default class Home extends Component {
    constructor(props) {
        super(props);
        this.state = {
            text: "我是默认值",
            arr: [],
            num: store.getState(),
        };
        axios.get("/api/101320101.html").then((res) => {
            console.log("15", res);
        });
    }
    dataFun = (text) => {
        console.log("子传给父", text);
        this.setState({
            text,
        });
    };
    componentDidMount() {
        this.ajaxfun();
        store.subscribe(() => {
            this.setState({
                num: store.getState(),
            });
        });
    }
    ajaxfun = () => {
        axios.get("http://localhost:4000/arr").then((res) => {
            console.log(res);
            this.setState({
                arr: res.data,
            });
        });
    };
    render() {
        return (
            <div>
                home <News text="你好我是home传给news的text" fufun={this.dataFun} /> <Phone />
                <div>
                    {this.state.arr.map((v, i) => {
                        return <p key={i}> {v.name} </p>;
                    })}
                </div>
                <div>hello{this.state.num}</div>
                <button
                    onClick={() => {
                        store.dispatch(action.add(1));
                    }}
                >
                    点我加1
                </button>
                <button
                    onClick={() => {
                        store.dispatch(action.del(1));
                    }}
                >
                    点我减1
                </button>
            </div>
        );
    }
}
